package com.example.shirotest.controller;

import com.example.shirotest.base.ResultVo;
import com.example.shirotest.console.GetRemoteData;
import com.example.shirotest.console.Maingeter;
import com.example.shirotest.entry.Result;
import com.example.shirotest.entry.Sites;
import com.example.shirotest.entry.TableList;
import com.example.shirotest.service.ResultService;
import com.example.shirotest.service.SitesService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/result")
@Controller
public class ResultController extends BashController {

    @Autowired
    private ResultService resultService;

    @Autowired
    Maingeter maingeter;

    @Autowired
    GetRemoteData getRemoteData;

    @Autowired
    SitesService sitesService;

    @RequestMapping("/getnet")
    @ResponseBody
    public ResultVo getNet(){
        maingeter.runGeter();
        return new ResultVo("code is running");
    }

    @RequestMapping("/cashdata")
    @ResponseBody
    public ResultVo cashdata(){
        HashMap uniqueMap = getRemoteData.getUniqueMap();
        return new ResultVo(uniqueMap);
    }

    @RequestMapping("/index")
    public String result(Model model){
        setUserInfo(model);

        List<Sites> list = sitesService.list();
        model.addAttribute("sitelist",list);

        model.addAttribute("tag_mb","result-index");
        return "result/index";
    }


    @RequestMapping("/list")
    @ResponseBody
    public TableList resultlist(HttpServletRequest request){
        Integer pagenum  = Integer.parseInt(request.getParameter("pagenum"));
        Integer pagesize  = Integer.parseInt(request.getParameter("pagesize"));
        Integer sid  = Integer.parseInt(request.getParameter("sid"));
        String  sort  = request.getParameter("sort");
        String  order  = request.getParameter("order");
        String  keywords = request.getParameter("keywords");

        PageInfo<Result> PageInfo = resultService.selectByPage(pagenum, pagesize, sort + " " + order,sid,keywords);

        //封装输出数据
        TableList<Result> TableList = new TableList<>();
        TableList.setList(PageInfo.getList());
        TableList.setTotal((int) PageInfo.getTotal());

        return TableList;
    }


    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public ResultVo delete(Integer id){
        if(resultService.removeById(id)){
            return new  ResultVo(1,"删除成功","");
        }

        return new  ResultVo(0,"删除失败","");
    }


}
